﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;
namespace BLLA
{
    public partial class Order_MerAccount_T : BaseBLL<Model.Order_MerAccount_T>, IBLL.IOrder_MerAccount_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        /// <summary>
        /// 根据商户ID获取账目
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="MerID">商户ID</param>
        /// <param name="MerAccType">流水类型</param>
        /// <param name="MerAccDate">yyyyMMdd</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_MerAccount(int pageIndex, int pageSize, string MerID, string MerAccType, string MerAccDate, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_MerAccount> mo = new List<Model.View_Order_MerAccount>();
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append( "select * from View_Order_MerAccount where MerID='" + MerID + "'and MerAccSate='" + 1 + "'");
                if (MerAccType != null &&  MerAccType!= "")
                {
                    sql.Append("and MerAccType='" + MerAccType + "'");
                }

                if (MerAccDate != null && MerAccDate != "")
                {
                    sql.Append("and MerAccDate='" + MerAccDate + "'");
                }
               mo = base.DBSession.IView_Order_MerAccountDAL.GetListBy(sql.ToString());

                //mo = base.DBSession.IView_Order_MerAccountDAL.GetListBy("select * from View_Order_MerAccount where MerID='" + MerID + "'and MerAccSate='" + 1 + "'");
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到用户！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.rows = mo.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            result.total = mo.Count;

            return os.StringOfObject(result, RETID);
            #endregion
        }

        public void CloseAccout()
        {
            List<Model.Order_Mer_T> MerList = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerState == 1);
            string time = DateTime.Now.AddDays(-1).ToString("yyyyMMddHHmmss");
            string time1 = DateTime.Now.ToString("HHmmss");
            int day = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
            foreach (var item in MerList)
            {
                List<Model.Order_Form_T> OrderList = base.DBSession.IOrder_Form_TDAL.GetListBy("SELECT * FROM  Order_Form_T where FormTime>'" + time.Substring(0, 8) + "000000" + "' and FormTime<'" + time.Substring(0, 8) + "235959" + "' and FormMerID='" + item.MerID + "'"); //商户一天为一条账目，分类订单组合成一条今日分类账目
                Model.Order_MerAccount_T QB = new Model.Order_MerAccount_T();//金额0
                int je = 0;
                QB.MerAccDate = day;
                QB.MerAccTime = int.Parse(time1);
                QB.MerAccType = 0;
                QB.MerAccSate = 1;
                QB.MerID = item.MerID;
                QB.MerAccOut = "";
                QB.MerAccBZ = "";
                Model.Order_MerAccount_T DSF = new Model.Order_MerAccount_T();//第三方2
                int dsf = 0;
                DSF.MerAccDate = day;
                DSF.MerAccTime = int.Parse(time1);
                DSF.MerAccType = 1;
                DSF.MerAccSate = 1;
                DSF.MerID = item.MerID;
                DSF.MerAccBZ = "";
                DSF.MerAccOut = "";
                Model.Order_MerAccount_T QY = new Model.Order_MerAccount_T();//企业3
                int qy = 0;
                QY.MerAccDate = day;
                QY.MerAccTime = int.Parse(time1);
                QY.MerAccType = 4;
                QB.MerAccSate = 1;
                QY.MerID = item.MerID;
                QY.MerAccOut = "";
                QY.MerAccBZ = "";
                Model.Order_MerAccount_T JF = new Model.Order_MerAccount_T();//积分4
                int jf = 0;
                JF.MerAccDate = day;
                JF.MerAccTime = int.Parse(time1);
                JF.MerAccType = 2;
                JF.MerAccSate = 1;
                JF.MerAccBZ = "";
                JF.MerAccOut = "";
                JF.MerID = item.MerID;
                //base.DBSession.IOrder_MerAccount_TDAL.Add(JF);
                if (OrderList.Count != 0)
                {
                    List<Model.Order_MerAccount_T> AccADD = new List<Model.Order_MerAccount_T>();
                    foreach (Model.Order_Form_T itm in OrderList)
                    {
                        switch (itm.FormPayType)
                        {
                            case 1://钱包1
                                je += itm.FormPayMon;
                                break;
                            case 2://第三方2
                                dsf += itm.FormPayMon;
                                break;
                            case 3://企业3
                                qy += itm.FormPayMon;
                                break;
                            case 4://积分4
                                jf += itm.FormPayMon;
                                break;
                        }
                    }
                    QB.MerAccInt = je.ToString();
                    AccADD.Add(QB);
                    DSF.MerAccInt = dsf.ToString();
                    AccADD.Add(DSF);
                    JF.MerAccInt = jf.ToString();
                    AccADD.Add(JF);
                    QY.MerAccInt = qy.ToString();
                    AccADD.Add(QY);
                    base.DBSession.IOrder_MerAccount_TDAL.Add(AccADD);
                }
            }
        }
    }
}
